import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_goods.dart';
import '../../../../bean/bean_user_account.dart';
import '../order_list_page.dart';
import '../widget/order_submit_widget.dart';
import 'account_center_controller.dart';
import 'account_center_details_controller.dart';

class GoodsCenterController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 0;
  int pageSize = 30;
  List<GoodsBean> goodsList = [];

  Future<bool> requestGoodsList() async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    int userId = userBean.userId;

    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.goodsListUrl, queryParameters: map2);
    goodsList = [];
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data;
      totalCount = data["totalCount"] ?? 0;
      totalPage = data["totalPage"] ?? 0;
      currPage = data["currPage"] ?? 0;
      pageSize = data["pageSize"] ?? 0;
      dynamic list = data["list"];
      if (list != null) {
        for (dynamic item in list) {
          GoodsBean bean = GoodsBean.fromMap(item);
          goodsList.add(bean);
        }
      }
    }
    update(["goodsList"]);
    return true;
  }

  Future<bool> onRefresh() async {
    await requestGoodsList();
    return true;
  }

  @override
  void onReady() {
    super.onReady();

    requestGoodsList();
  }

  bool submitOrderLoading = false;

  void goShop(GoodsBean item, String tag, BuildContext context) async {
    AccountCenterController accountCenterController =
        Get.find<AccountCenterController>(tag: tag);

    if (Get.isRegistered<AccountCenterDetailsController>(tag: tag)) {
      AccountCenterDetailsController detailsController =
          Get.find<AccountCenterDetailsController>(tag: tag);
      UserAccountStateBean accountStateBean =
          detailsController.userAccountStateBean;
      num remainAmount = accountStateBean.accountBean.remainAmount;
      num iteNumber = item.unitNumber;
      if (remainAmount < iteNumber) {
        ToastUtils.showToast("您的积分不足，暂时无法兑换，快去答题获取积分吧");
        return;
      }
      dynamic result = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return OrderSubmitWidget(iteNumber, remainAmount - iteNumber,
              item.goodsName, item.goodsContent);
        },
      );

      if (result == null || !result) {
        return;
      }
    }

    submitOrderLoading = true;
    update(["submitOrder${item.id}"]);
    Map<String, dynamic> map2 = {};
    map2["goodId"] = item.id;
    ResponseInfo responseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.orderSubmitUrl, jsonMap: map2);
    goodsList = [];
    submitOrderLoading = false;
    update(["submitOrder${item.id}"]);
    if (responseInfo.success) {
      //解析数据
      ToastUtils.showToast("下单成功");
      Get.off(() => OrderListPage());
    } else {
      Get.defaultDialog(
        title: "下单失败",
        content: Text("${responseInfo.message}"),
        radius: 8,
      );
    }
  }
}
